package me.chan.sort;

import static me.chan.util.SortUtil.*;
/**
 * Created by JanGin on 2017/9/10.
 */
public class SelectionSort {



    public static void sort(Comparable[] arr) {

        long start = System.nanoTime();

        for(int i=0, n = arr.length; i < n; i++) {
            for (int j=i+1; j < n; j++) {
                if ( less(arr[j], arr[i]) ) {
                    exch(arr, i, j);
                }
            }
        }

        //it costs:55832ns
        System.out.println("it costs:"+(System.nanoTime() - start)+"ns");

    }

    public static void sortImprovement(Comparable[] arr) {
        long start  = System.nanoTime();

        for (int i=0; i < arr.length; ++i) {

            int k = i;

            for (int j=i+1; j < arr.length; ++j) {
                if ( less(arr[j], arr[k]) ) {
                    k = j;
                }
            }

            exch(arr, i, k);
        }

        //it costs:24632ns
        System.out.println("it costs:" + (System.nanoTime() - start) + "ns");
    }

    public static void main(String[] args) {
        Integer[] arr = {20, 17, 12, 9, 10, 23, 13, 24, 19, 93, 8, 15 };
        //before sorted
        show(arr);  //[ 20, 17, 12, 9, 10, 23, 13, 24, 19, 93, 8, 15 ]

        sort(arr);

        sortImprovement(arr);
        //after sorted
        show(arr);  //[ 8, 9, 10, 12, 13, 15, 17, 19, 20, 23, 24, 93 ]

    }

}
